package com.matt.controller;


import com.dotflat.fin.utils.excelUtil.ExcelUtil;
import com.matt.domain.AboutPallet;
import com.matt.domain.AboutVisit;
import com.matt.foundation.excel.AboutPalletExcel;
import com.matt.foundation.excel.AboutVisitExcel;
import com.matt.foundation.model.StatusResponse;
import com.matt.foundation.query.AboutPalletQuery;
import com.matt.foundation.query.AboutVisitQuery;
import com.matt.service.AboutVisitService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 预约到访控制层
 *
 * @author makejava
 * @since 2025-02-20 11:01:26
 */
@RestController
@RequestMapping("/aboutVisit")
@AllArgsConstructor
public class AboutVisitController {

	private AboutVisitService aboutVisitService;

	/**
	 * 获取预约到访(试听)列表(分页)
	 */
	@PostMapping("/list")
	public StatusResponse list(@RequestHeader String ticket, @RequestBody AboutVisitQuery aboutVisit) {
		return StatusResponse.success(aboutVisitService.list(ticket,aboutVisit));
	}


	/**
	 * 获取预约到访列表
	 */
	@PostMapping("/getList")
	public StatusResponse getList(@RequestBody AboutVisitQuery aboutVisit) {
		return StatusResponse.success(aboutVisitService.getList(aboutVisit));
	}


	@ApiOperation(value = "预约到访",httpMethod = "GET")
	@GetMapping("/deriveVisit")
	public void derivePallet(HttpServletResponse response, AboutVisitQuery classesQuery, @RequestHeader String ticket) throws IOException {
		classesQuery.setPageNum(1);
		classesQuery.setPageSize(1000);
		Page<AboutVisit> aboutVisits = aboutVisitService.list(ticket,classesQuery);
		List<AboutVisitExcel> aboutVisitExcels = new ArrayList<>();
		if(!aboutVisits.getContent().isEmpty()){
			aboutVisits.forEach(a->{
				AboutVisitExcel palletExcel = new AboutVisitExcel();
				BeanUtils.copyProperties(a,palletExcel);
				switch(a.getStatus()){
					case 1 : palletExcel.setStatus("已到访");break;
					case 2 : palletExcel.setStatus("未到访");break;
					default : palletExcel.setStatus("_");
				}
				aboutVisitExcels.add(palletExcel);
			});
		}
		ExcelUtil<AboutVisitExcel> excelUtil = new ExcelUtil<AboutVisitExcel>(AboutVisitExcel.class);
		excelUtil.exportExcel(response,aboutVisitExcels,"预约到访导出","预约到访导出");
	}

	/**
	 * 获取预约到访
	 */
	@PostMapping("/get")
	public StatusResponse get(@RequestParam("id") String id) {
		AboutVisit aboutVisit = aboutVisitService.findById(id);
		return StatusResponse.success(aboutVisit);
	}

	/**
	 * 添加预约到访
	 */
	@PostMapping("/add")
	public StatusResponse add(@RequestHeader String ticket,@RequestBody AboutVisit aboutVisit) {

		return StatusResponse.success(aboutVisitService.save(ticket,aboutVisit));
	}


	/**
	 * 添加预约到访
	 */
	@PostMapping("/wpAdd")
	public StatusResponse wpAdd(@RequestBody AboutVisit aboutVisit) {

		return StatusResponse.success(aboutVisitService.wpAdd(aboutVisit));
	}

	/**
	 * 添加预约到访
	 */
	@PostMapping("/wpUpdate")
	public StatusResponse wpUpdate(@RequestBody AboutVisit aboutVisit) {

		return StatusResponse.success(aboutVisitService.wpUpdate(aboutVisit));
	}

	/**
	 * 修改预约到访
	 */
	@PostMapping("/update")
	public StatusResponse update(@RequestHeader String ticket,@RequestBody AboutVisit aboutVisit) {
		return StatusResponse.success(aboutVisitService.update(ticket,aboutVisit));
	}


	@PostMapping("/updateLeads")
	public StatusResponse updateLeads(@RequestHeader String ticket,@RequestBody AboutVisit aboutVisit) {
		return aboutVisitService.updateLeads(ticket,aboutVisit);
	}


	/**
	 * 删除预约到访
	 */
	@PostMapping("/delete")
	public void delete(String id) {
		aboutVisitService.deleteById(id);
	}

}

